//
//  CodeVC.m
//  Project
//
//  Created by 冯东辉 on 2018/9/30.
//  Copyright © 2018年 FengDongHui. All rights reserved.
//

#import "CodeVC.h"
#import <WXApi.h>

@interface CodeVC ()

@property (nonatomic, strong) UIView *baseShareV;

@end

@implementation CodeVC

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navTitleL.text = @"应用分享";
    [self createBlueNavigationBar:YES];
    
    UIButton *rightBtn = [[UIButton alloc] initWithFrame:CGRectMake(GET_SCREEN_WIDTH()-44, SafeAreaTopHeight-44, 44, 44)];
    [rightBtn setImage:[UIImage imageNamed:@"share"] forState:UIControlStateNormal];
    [rightBtn addTarget:self action:@selector(share) forControlEvents:UIControlEventTouchUpInside];
    
    UIBarButtonItem *rightBarItem = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    self.navigationItem.rightBarButtonItem = rightBarItem;
    
    [self creatSubViews];
    
}

- (void)didReceiveMemoryWarning {
    
    [super didReceiveMemoryWarning];
}

- (void)creatSubViews {
    
    CIFilter *filter = [CIFilter filterWithName:@"CIQRCodeGenerator"];
    [filter setDefaults];
    NSString *urlStr = @"http://111.8.20.247:89/UploadFiles/download.html";
    NSData *data = [urlStr dataUsingEncoding:NSUTF8StringEncoding];
    [filter setValue:data forKey:@"inputMessage"];
    CIImage *outputImage = [filter outputImage];
    
    CGFloat width = GET_SCREEN_WIDTH()-80;
    
    UIImageView *wechatImageView = [[UIImageView alloc] initWithFrame:CGRectMake(40, SafeAreaTopHeight+(GET_SCREEN_HEIGHT()-SafeAreaTopHeight-width)/2.0f - 20, width, width)];
    wechatImageView.image = [self createNonInterpolatedUIImageFormCIImage:outputImage withSize:width];//重绘二维码,使其显示清晰
    [self.view addSubview:wechatImageView];
    
    UILabel *noteL = [[UILabel alloc] initWithFrame:CGRectMake(0, wechatImageView.bottom+20, GET_SCREEN_WIDTH(), 20)];
    noteL.text = @"扫码下载APP";
    noteL.font = [UIFont systemFontOfSize:15];
    noteL.textColor = UICOLOR_HEX(0xff333333);
    noteL.textAlignment = NSTextAlignmentCenter;
    [self.view addSubview:noteL];
}

/**
 * 根据CIImage生成指定大小的UIImage
 *
 * @param image CIImage
 * @param size 图片宽度
 */
- (UIImage *)createNonInterpolatedUIImageFormCIImage:(CIImage *)image withSize:(CGFloat) size {
    
    CGRect extent = CGRectIntegral(image.extent);
    CGFloat scale = MIN(size/CGRectGetWidth(extent), size/CGRectGetHeight(extent));
    // 1.创建bitmap;
    size_t width = CGRectGetWidth(extent) * scale;
    size_t height = CGRectGetHeight(extent) * scale;
    CGColorSpaceRef cs = CGColorSpaceCreateDeviceGray();
    CGContextRef bitmapRef = CGBitmapContextCreate(nil, width, height, 8, 0, cs, (CGBitmapInfo)kCGImageAlphaNone);
    CIContext *context = [CIContext contextWithOptions:nil];
    CGImageRef bitmapImage = [context createCGImage:image fromRect:extent];
    CGContextSetInterpolationQuality(bitmapRef, kCGInterpolationNone);
    CGContextScaleCTM(bitmapRef, scale, scale);
    CGContextDrawImage(bitmapRef, extent, bitmapImage);
    // 2.保存bitmap到图片
    CGImageRef scaledImage = CGBitmapContextCreateImage(bitmapRef);
    CGContextRelease(bitmapRef);
    CGImageRelease(bitmapImage);
    return [UIImage imageWithCGImage:scaledImage];
}

#pragma mark - 分享
- (void)share {
    
    if (_baseShareV) {
        
        _baseShareV.hidden = NO;
        
        return;
    }
    
    _baseShareV = [[UIView alloc] initWithFrame:CGRectMake(0, 0, GET_SCREEN_WIDTH(), GET_SCREEN_HEIGHT())];
    _baseShareV.backgroundColor = [UICOLOR_HEX(0xff000000) colorWithAlphaComponent:0.5f];
    [self.view.window addSubview:_baseShareV];
    
    UIButton *hyBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, GET_SCREEN_HEIGHT()-50-130, GET_SCREEN_WIDTH()/2.0f, 130)];
    hyBtn.backgroundColor = UICOLOR_HEX(0xffffffff);
    [hyBtn setImage:[UIImage imageNamed:@"share_wxhy"] forState:UIControlStateNormal];
    hyBtn.tag = 1000;
    [hyBtn addTarget:self action:@selector(wxShare:) forControlEvents:UIControlEventTouchUpInside];
    [_baseShareV addSubview:hyBtn];
    
    UIButton *pyqBtn = [[UIButton alloc] initWithFrame:CGRectMake(hyBtn.right, hyBtn.top, hyBtn.width, hyBtn.height)];
    pyqBtn.backgroundColor = UICOLOR_HEX(0xffffffff);
    [pyqBtn setImage:[UIImage imageNamed:@"share_pyq"] forState:UIControlStateNormal];
    pyqBtn.tag = 2000;
    [pyqBtn addTarget:self action:@selector(wxShare:) forControlEvents:UIControlEventTouchUpInside];
    [_baseShareV addSubview:pyqBtn];
    
    UIButton *cancelBtn = [[UIButton alloc] initWithFrame:CGRectMake(0, hyBtn.bottom, GET_SCREEN_WIDTH(), 50)];
    cancelBtn.backgroundColor = UICOLOR_HEX(0xffEDEEEF);
    [cancelBtn setTitle:@"取消" forState:UIControlStateNormal];
    [cancelBtn setTitleColor:UICOLOR_HEX(0xff000000) forState:UIControlStateNormal];
    cancelBtn.titleLabel.font = [UIFont systemFontOfSize:18];
    [cancelBtn addTarget:self action:@selector(hiddenShareView) forControlEvents:UIControlEventTouchUpInside];
    [_baseShareV addSubview:cancelBtn];
}

- (void)hiddenShareView {
    
    _baseShareV.hidden = YES;
}

- (void)wxShare:(UIButton *)btn {
    
    [self hiddenShareView];
    
    WXMediaMessage *message = [WXMediaMessage message];
    // 设置消息缩略图的方法
    // [message setThumbImage:[UIImage imageNamed:@"图片.png"]];
    
    // 多媒体消息中包含的图片数据对象
    WXImageObject *imageObject = [WXImageObject object];
    // 图片真实数据内容
    NSData *data = UIImagePNGRepresentation([self captureImageFromView]);
    imageObject.imageData = data;
    // 多媒体数据对象，可以为WXImageObject，WXMusicObject，WXVideoObject，WXWebpageObject等。
    message.mediaObject = imageObject;
    
    SendMessageToWXReq *req = [[SendMessageToWXReq alloc] init];
    req.bText = NO;
    req.message = message;
    if (btn.tag == 1000) {
        req.scene = WXSceneSession;
    }else {
        req.scene = WXSceneTimeline;
    }
    
    [WXApi sendReq:req];
}

-(UIImage *)captureImageFromView {
    
    UIGraphicsBeginImageContextWithOptions(self.view.frame.size,NO, 0);
    
    [[UIColor clearColor] setFill];
    
    [[UIBezierPath bezierPathWithRect:self.view.bounds] fill];
    
    CGContextRef ctx = UIGraphicsGetCurrentContext();
    
    [self.view.layer renderInContext:ctx];
    
    UIImage *image = UIGraphicsGetImageFromCurrentImageContext();
    
    UIGraphicsEndImageContext();
    
    return image;
}

@end
